package com.zhengjiang.sms.producer.controller;

import com.zhengjiang.sms.common.Const;
import com.zhengjiang.sms.common.ResponseDto;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

/**
 * @ClassName SmsSenderController
 * @Description
 * @Author zhengjiang(99514925 @ qq.com)
 * @Date 2020-05-28 14:02
 * @Version V1.0
 **/
@RestController
@RequestMapping("sms")
public class SmsSenderController {

    @Autowired
    private AmqpTemplate rabbitTemplate;

    @RequestMapping("/sendCode")
    public ResponseDto sendCode(@RequestParam("phone") String phone){
        Map<String,String> map = new HashMap<String, String>();
        map.put("phone",phone);
        map.put("code",getCode());
        rabbitTemplate.convertAndSend(Const.SMS_DIRECT_EXCHANGE,Const.SMS_ROUTING_KEY,map.toString());
        return ResponseDto.builder().code(200).message("success").build();
    }


    /**
     * 功能描述:获取6位数的短信验证码
     * @Param :
     * @Return:
     * @Date: 2020-05-29 14:10
    */
    private String getCode (){
       return String.valueOf(new Random().nextInt(899999) + 100000);
    }
}
